package cn.rulian.car.outbill.controller;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.rulian.base.auth.ann.Access;
import cn.rulian.base.auth.bo.User;
import cn.rulian.base.common.dto.Message;
import cn.rulian.base.common.dto.UserInfo;
import cn.rulian.base.common.util.xx;
import cn.rulian.base.common.vo.Result;
import cn.rulian.car.outbill.bo.OutBill;
import cn.rulian.car.outbill.bo.OutBillFlow;
import cn.rulian.car.outbill.dao.OutBillStateDao;
import cn.rulian.car.outbill.service.OutBillFlowService;
import cn.rulian.car.outbill.service.OutBillService;
import cn.rulian.car.outbill.vo.OutBillVo;

/**
 * 车辆外出单--审核
 */

@Access(authCode="outBillAudit")
@Controller
@RequestMapping("/car/outbill/outBillAudit")
public class OutBillAuditController {
	
	@Autowired
	private OutBillService service;

	@Autowired
	private OutBillStateDao outBillStateDao;
	
	@Autowired
	private OutBillFlowService outBillFlowService; 
	
	//private final Logger log = LoggerFactory.getLogger(getClass());
	
	private final String path="car/outbill/outbillAudit_"; //页面基本路径

	/**
	 * 页面基本路径
	 */
	private String getPath()
	{
		return path;
	}
	
	/**
	 * 列表页面
	 */
	@RequestMapping("/listPage")
	public String listPage() {
		return this.getPath()+"list";
	}

	/**
	 * 查询
	 */
	@RequestMapping("/listData")
	@ResponseBody
	public Result listData(HttpSession session,OutBillVo vo) {
		//取当前用户
		UserInfo userInfo = UserInfo.getUserInfo(session); 
		User user = userInfo.getUser();
		String organCode = user.getOrgan().getAllCode();
		
		return service.queryAudit(vo,organCode);
	}

	/**
	 * 修改页面
	 */
	@RequestMapping("/updatePage")
	public String updatePage(Model model, Long id) {
		model.addAttribute("bo", service.findOne(id));
		return this.getPath()+"update";
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@ResponseBody
	public Message update(HttpSession session,Long id,boolean auditResult,String opeInfo) {
		Message rm = new Message();
		try {
			//取当前用户
			UserInfo userInfo = UserInfo.getUserInfo(session); 
			User user = userInfo.getUser();
			String ope ="审核通过";
			
			OutBill bo = service.findOne(id);
			if(auditResult)
			{//审核通过
				bo.setState(outBillStateDao.findFirstByCode("07")); //状态：待回录
				if(xx.isEmpty(opeInfo))
				{
					opeInfo="同意";
				}
			}
			else
			{//退回
				bo.setState(outBillStateDao.findFirstByCode("04")); //状态：退回
				if(xx.isEmpty(opeInfo))
				{
					opeInfo="不同意";
				}
				ope ="退回";
			}
			
			bo.setApprover(user.getName());
			bo.setOpeTimeNear(new Date());
			
			OutBillFlow flow = new OutBillFlow();
			flow.setOutBill(bo);
			flow.setOpe(ope);
			flow.setOpeUserName(user.getName());
			flow.setOpeTime(new Date());
			flow.setOpeInfo(opeInfo);
			
			service.save(bo);
			outBillFlowService.save(flow);
			
			rm.setMsg("保存成功！");
			rm.setCode(Message._SUCCESS);
		} catch (Exception e) {
			rm.setMsg("保存失败，异常信息：<br/>" + e.toString());
			rm.setCode(Message._ERROR);
			e.printStackTrace();
		}
		return rm;
	}
	
	
}
